﻿namespace DotRas
{
    using DotRas.Design;
    using System;
    using System.Collections.ObjectModel;
    using System.Reflection;

    public sealed class RasAutoDialAddressCollection : RasCollection<RasAutoDialAddress>
    {
        private RasAutoDialAddressItemCollection _lookUpTable = new RasAutoDialAddressItemCollection();

        internal RasAutoDialAddressCollection()
        {
        }

        protected override void ClearItems()
        {
            while (base.Count > 0)
            {
                base.RemoveAt(0);
            }
        }

        protected override void InsertItem(int index, RasAutoDialAddress item)
        {
            if (base.IsInitializing)
            {
                this._lookUpTable.Insert(index, item);
            }
            else if (RasHelper.SetAutoDialAddress(item.Address, item.Entries))
            {
                item = RasHelper.GetAutoDialAddress(item.Address);
                this._lookUpTable.Insert(index, item);
            }
            base.InsertItem(index, item);
        }

        internal void Load()
        {
            try
            {
                base.BeginLock();
                base.IsInitializing = true;
                base.Clear();
                Collection<string> autoDialAddresses = RasHelper.GetAutoDialAddresses();
                if ((autoDialAddresses != null) && (autoDialAddresses.Count > 0))
                {
                    for (int i = 0; i < autoDialAddresses.Count; i++)
                    {
                        RasAutoDialAddress autoDialAddress = RasHelper.GetAutoDialAddress(autoDialAddresses[i]);
                        if (autoDialAddress != null)
                        {
                            base.Add(autoDialAddress);
                        }
                    }
                }
            }
            finally
            {
                base.IsInitializing = false;
                base.EndLock();
            }
        }

        protected override void RemoveItem(int index)
        {
            if (!base.IsInitializing)
            {
                RasAutoDialAddress address = base[index];
                if ((address != null) && RasHelper.SetAutoDialAddress(address.Address, null))
                {
                    this._lookUpTable.RemoveAt(index);
                }
            }
            base.RemoveItem(index);
        }

        public RasAutoDialAddress this[string address]
        {
            get
            {
                return this._lookUpTable[address];
            }
        }

        private class RasAutoDialAddressItemCollection : KeyedCollection<string, RasAutoDialAddress>
        {
            protected override string GetKeyForItem(RasAutoDialAddress item)
            {
                if (item == null)
                {
                    DotRas.ThrowHelper.ThrowArgumentNullException("item");
                }
                return item.Address;
            }
        }
    }
}

